package gui;

import javax.swing.text.*;  
import java.awt.Toolkit;  

/**
 * @author Matthias
 *
 */
public class LimitOfChar extends DocumentFilter {  
	  
    private int max;  
  
    public LimitOfChar(int maxLength) {  
        max = maxLength;  
    }  
  
    public void insertString(DocumentFilter.FilterBypass fb, int offset,  
            String text, AttributeSet attr) throws BadLocationException {  
        if (fb.getDocument().getLength() + text.length() <= max)  
               fb.insertString(offset, text, attr);  
          else Toolkit.getDefaultToolkit().beep();  
    }  

    public void replace(DocumentFilter.FilterBypass fb, int offset, int length,  
            String text, AttributeSet attr) throws BadLocationException {  
        if (fb.getDocument().getLength() + text.length() - length <= max)  
               fb.replace(offset, length, text, attr);  
          else Toolkit.getDefaultToolkit().beep();  
    }  
}  
